{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# 导入"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {},
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "d:\\softwares\\python\\lib\\site-packages\\umap\\distances.py:1063: NumbaDeprecationWarning: \u001b[1mThe 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.\u001b[0m\n",
            "  @numba.jit()\n",
            "d:\\softwares\\python\\lib\\site-packages\\umap\\distances.py:1071: NumbaDeprecationWarning: \u001b[1mThe 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.\u001b[0m\n",
            "  @numba.jit()\n",
            "d:\\softwares\\python\\lib\\site-packages\\umap\\distances.py:1086: NumbaDeprecationWarning: \u001b[1mThe 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.\u001b[0m\n",
            "  @numba.jit()\n",
            "d:\\softwares\\python\\lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
            "  from .autonotebook import tqdm as notebook_tqdm\n",
            "d:\\softwares\\python\\lib\\site-packages\\umap\\umap_.py:660: NumbaDeprecationWarning: \u001b[1mThe 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.\u001b[0m\n",
            "  @numba.jit()\n"
          ]
        }
      ],
      "source": [
        "from bertopic import BERTopic\n",
        "from sentence_transformers import SentenceTransformer\n",
        "from umap import UMAP"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# 加载数据"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {},
      "outputs": [],
      "source": [
        "# step1 加载文件\n",
        "docs = [\n",
        "  '今天天气很好',\n",
        "  '今天天气很不错',\n",
        "  '今天天气晴朗',\n",
        "  '今天天气不错',\n",
        "  '今天阳光明媚',\n",
        "  '自然语言处理会使用到深度学习',\n",
        "  '自然语言处理会使用到线性代数',\n",
        "  '自然语言处理会需要掌握一定的微积分知识',\n",
        "  '自然语言处理很简单',\n",
        "  '自然语言处理容易掌握'\n",
        "]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# 创建词向量模型"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "SentenceTransformer(\n",
              "  (0): Transformer({'max_seq_length': 128, 'do_lower_case': False}) with Transformer model: BertModel \n",
              "  (1): Pooling({'word_embedding_dimension': 384, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False})\n",
              ")"
            ]
          },
          "execution_count": 3,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# 方式1：从网络中下载\n",
        "embedding_model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')\n",
        "\n",
        "# 方式2：从本地加载，注意\\\\转义\n",
        "# 路径写到包含config.json的位置\n",
        "# embedding_model = SentenceTransformer('C:\\\\Users\\\\ji\\\\.cache\\\\huggingface\\\\hub\\\\models--sentence-transformers--paraphrase-multilingual-MiniLM-L12-v2\\\\snapshots\\\\bf3bf13ab40c3157080a7ab344c831b9ad18b5eb')\n",
        "embedding_model"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# 训练词向量"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {},
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "Batches: 100%|██████████| 1/1 [00:00<00:00,  7.56it/s]"
          ]
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "<class 'numpy.ndarray'> (10, 384)\n",
            "[[ 0.22831464  0.20791563 -0.01361518 ...  0.08457322 -0.27376777\n",
            "   0.30211884]\n",
            " [ 0.23415914  0.18763618 -0.00334522 ...  0.07795675 -0.28823084\n",
            "   0.29783475]\n",
            " [ 0.25473982  0.1818737   0.02977072 ...  0.12291708 -0.15997928\n",
            "   0.20394088]\n",
            " ...\n",
            " [-0.10149594  0.03836722 -0.28412744 ...  0.23825662 -0.04146777\n",
            "  -0.19284798]\n",
            " [-0.28807735 -0.02647653  0.18795004 ...  0.48011842  0.34286204\n",
            "  -0.21912694]\n",
            " [-0.1399834  -0.17367244 -0.00831866 ...  0.33865795  0.19647521\n",
            "  -0.17728156]]\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\n"
          ]
        }
      ],
      "source": [
        "embeddings = embedding_model.encode(docs, show_progress_bar=True)\n",
        "print(type(embeddings), embeddings.shape)\n",
        "print(embeddings)\n",
        "# 这里生成的六个语义向量，对前面的六个句子"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# 文本聚类 ⭐"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {},
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "2024-04-19 09:34:02,442 - BERTopic - Reduced dimensionality\n",
            "2024-04-19 09:34:02,446 - BERTopic - Clustered reduced embeddings\n"
          ]
        },
        {
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Topic</th>\n",
              "      <th>Count</th>\n",
              "      <th>Name</th>\n",
              "      <th>Representation</th>\n",
              "      <th>Representative_Docs</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>0</td>\n",
              "      <td>5</td>\n",
              "      <td>0_今天阳光明媚_今天天气晴朗_今天天气很好_今天天气很不错</td>\n",
              "      <td>[今天阳光明媚, 今天天气晴朗, 今天天气很好, 今天天气很不错, 今天天气不错, , , ...</td>\n",
              "      <td>[今天天气晴朗, 今天天气很不错, 今天天气很好]</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>1</td>\n",
              "      <td>5</td>\n",
              "      <td>1_自然语言处理很简单_自然语言处理容易掌握_自然语言处理会需要掌握一定的微积分知识_自然语...</td>\n",
              "      <td>[自然语言处理很简单, 自然语言处理容易掌握, 自然语言处理会需要掌握一定的微积分知识, 自...</td>\n",
              "      <td>[自然语言处理容易掌握, 自然语言处理会需要掌握一定的微积分知识, 自然语言处理会使用到线性代数]</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "   Topic  Count                                               Name  \\\n",
              "0      0      5                     0_今天阳光明媚_今天天气晴朗_今天天气很好_今天天气很不错   \n",
              "1      1      5  1_自然语言处理很简单_自然语言处理容易掌握_自然语言处理会需要掌握一定的微积分知识_自然语...   \n",
              "\n",
              "                                      Representation  \\\n",
              "0  [今天阳光明媚, 今天天气晴朗, 今天天气很好, 今天天气很不错, 今天天气不错, , , ...   \n",
              "1  [自然语言处理很简单, 自然语言处理容易掌握, 自然语言处理会需要掌握一定的微积分知识, 自...   \n",
              "\n",
              "                                 Representative_Docs  \n",
              "0                          [今天天气晴朗, 今天天气很不错, 今天天气很好]  \n",
              "1  [自然语言处理容易掌握, 自然语言处理会需要掌握一定的微积分知识, 自然语言处理会使用到线性代数]  "
            ]
          },
          "execution_count": 5,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# 创建模型\n",
        "topic_model = BERTopic(\n",
        "  embedding_model=embedding_model,\n",
        "  min_topic_size=2, # 重要参数\n",
        "  verbose=True,\n",
        ")\n",
        "\n",
        "# 训练模型\n",
        "topic_model.fit_transform(docs, embeddings=embeddings)\n",
        "\n",
        "# 获取聚类结果\n",
        "topic_info = topic_model.get_topic_info()\n",
        "topic_info"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# 可视化"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "application/vnd.plotly.v1+json": {
              "config": {
                "plotlyServerURL": "https://plot.ly"
              },
              "data": [
                {
                  "hoverinfo": "text",
                  "hovertext": [
                    null
                  ],
                  "marker": {
                    "color": "#CFD8DC",
                    "opacity": 0.5,
                    "size": 5
                  },
                  "mode": "markers+text",
                  "name": "other",
                  "showlegend": false,
                  "type": "scattergl",
                  "x": [
                    null
                  ],
                  "y": [
                    null
                  ]
                },
                {
                  "hoverinfo": "text",
                  "hovertext": [
                    "今天天气晴朗",
                    "今天天气很好",
                    "今天天气很不错",
                    "今天天气不错",
                    "今天阳光明媚"
                  ],
                  "marker": {
                    "opacity": 0.5,
                    "size": 5
                  },
                  "mode": "markers+text",
                  "name": "0_今天阳光明媚_今天天气晴朗_今天天气很好",
                  "text": [
                    "",
                    "",
                    "",
                    "",
                    ""
                  ],
                  "textfont": {
                    "size": 12
                  },
                  "type": "scattergl",
                  "x": [
                    8.416213035583496,
                    8.09355640411377,
                    7.765477180480957,
                    7.397598743438721,
                    7.7361297607421875
                  ],
                  "y": [
                    -11.65169906616211,
                    -12.255067825317383,
                    -12.589025497436523,
                    -12.086901664733887,
                    -11.686054229736328
                  ]
                },
                {
                  "hoverinfo": "text",
                  "hovertext": [
                    "自然语言处理很简单",
                    "自然语言处理会使用到深度学习",
                    "自然语言处理容易掌握",
                    "自然语言处理会使用到线性代数",
                    "自然语言处理会需要掌握一定的微积分知识"
                  ],
                  "marker": {
                    "opacity": 0.5,
                    "size": 5
                  },
                  "mode": "markers+text",
                  "name": "1_自然语言处理很简单_自然语言处理容易掌握_自然语言处理会需要掌握一定的微积分知识",
                  "text": [
                    "",
                    "",
                    "",
                    "",
                    ""
                  ],
                  "textfont": {
                    "size": 12
                  },
                  "type": "scattergl",
                  "x": [
                    22.251264572143555,
                    22.507612228393555,
                    23.381763458251953,
                    22.907543182373047,
                    23.067338943481445
                  ],
                  "y": [
                    7.036581516265869,
                    7.683709621429443,
                    7.063472270965576,
                    6.714722633361816,
                    7.627180099487305
                  ]
                }
              ],
              "layout": {
                "annotations": [
                  {
                    "showarrow": false,
                    "text": "D1",
                    "x": 6.287958931922913,
                    "y": -2.820556628704071,
                    "yshift": 10
                  },
                  {
                    "showarrow": false,
                    "text": "D2",
                    "x": 16.58849345445633,
                    "xshift": 10,
                    "y": 8.83626606464386
                  }
                ],
                "height": 750,
                "shapes": [
                  {
                    "line": {
                      "color": "#CFD8DC",
                      "width": 2
                    },
                    "type": "line",
                    "x0": 16.58849345445633,
                    "x1": 16.58849345445633,
                    "y0": -14.477379322052002,
                    "y1": 8.83626606464386
                  },
                  {
                    "line": {
                      "color": "#9E9E9E",
                      "width": 2
                    },
                    "type": "line",
                    "x0": 6.287958931922913,
                    "x1": 26.889027976989745,
                    "y0": -2.820556628704071,
                    "y1": -2.820556628704071
                  }
                ],
                "template": {
                  "data": {
                    "bar": [
                      {
                        "error_x": {
                          "color": "rgb(36,36,36)"
                        },
                        "error_y": {
                          "color": "rgb(36,36,36)"
                        },
                        "marker": {
                          "line": {
                            "color": "white",
                            "width": 0.5
                          },
                          "pattern": {
                            "fillmode": "overlay",
                            "size": 10,
                            "solidity": 0.2
                          }
                        },
                        "type": "bar"
                      }
                    ],
                    "barpolar": [
                      {
                        "marker": {
                          "line": {
                            "color": "white",
                            "width": 0.5
                          },
                          "pattern": {
                            "fillmode": "overlay",
                            "size": 10,
                            "solidity": 0.2
                          }
                        },
                        "type": "barpolar"
                      }
                    ],
                    "carpet": [
                      {
                        "aaxis": {
                          "endlinecolor": "rgb(36,36,36)",
                          "gridcolor": "white",
                          "linecolor": "white",
                          "minorgridcolor": "white",
                          "startlinecolor": "rgb(36,36,36)"
                        },
                        "baxis": {
                          "endlinecolor": "rgb(36,36,36)",
                          "gridcolor": "white",
                          "linecolor": "white",
                          "minorgridcolor": "white",
                          "startlinecolor": "rgb(36,36,36)"
                        },
                        "type": "carpet"
                      }
                    ],
                    "choropleth": [
                      {
                        "colorbar": {
                          "outlinewidth": 1,
                          "tickcolor": "rgb(36,36,36)",
                          "ticks": "outside"
                        },
                        "type": "choropleth"
                      }
                    ],
                    "contour": [
                      {
                        "colorbar": {
                          "outlinewidth": 1,
                          "tickcolor": "rgb(36,36,36)",
                          "ticks": "outside"
                        },
                        "colorscale": [
                          [
                            0,
                            "#440154"
                          ],
                          [
                            0.1111111111111111,
                            "#482878"
                          ],
                          [
                            0.2222222222222222,
                            "#3e4989"
                          ],
                          [
                            0.3333333333333333,
                            "#31688e"
                          ],
                          [
                            0.4444444444444444,
                            "#26828e"
                          ],
                          [
                            0.5555555555555556,
                            "#1f9e89"
                          ],
                          [
                            0.6666666666666666,
                            "#35b779"
                          ],
                          [
                            0.7777777777777778,
                            "#6ece58"
                          ],
                          [
                            0.8888888888888888,
                            "#b5de2b"
                          ],
                          [
                            1,
                            "#fde725"
                          ]
                        ],
                        "type": "contour"
                      }
                    ],
                    "contourcarpet": [
                      {
                        "colorbar": {
                          "outlinewidth": 1,
                          "tickcolor": "rgb(36,36,36)",
                          "ticks": "outside"
                        },
                        "type": "contourcarpet"
                      }
                    ],
                    "heatmap": [
                      {
                        "colorbar": {
                          "outlinewidth": 1,
                          "tickcolor": "rgb(36,36,36)",
                          "ticks": "outside"
                        },
                        "colorscale": [
                          [
                            0,
                            "#440154"
                          ],
                          [
                            0.1111111111111111,
                            "#482878"
                          ],
                          [
                            0.2222222222222222,
                            "#3e4989"
                          ],
                          [
                            0.3333333333333333,
                            "#31688e"
                          ],
                          [
                            0.4444444444444444,
                            "#26828e"
                          ],
                          [
                            0.5555555555555556,
                            "#1f9e89"
                          ],
                          [
                            0.6666666666666666,
                            "#35b779"
                          ],
                          [
                            0.7777777777777778,
                            "#6ece58"
                          ],
                          [
                            0.8888888888888888,
                            "#b5de2b"
                          ],
                          [
                            1,
                            "#fde725"
                          ]
                        ],
                        "type": "heatmap"
                      }
                    ],
                    "heatmapgl": [
                      {
                        "colorbar": {
                          "outlinewidth": 1,
                          "tickcolor": "rgb(36,36,36)",
                          "ticks": "outside"
                        },
                        "colorscale": [
                          [
                            0,
                            "#440154"
                          ],
                          [
                            0.1111111111111111,
                            "#482878"
                          ],
                          [
                            0.2222222222222222,
                            "#3e4989"
                          ],
                          [
                            0.3333333333333333,
                            "#31688e"
                          ],
                          [
                            0.4444444444444444,
                            "#26828e"
                          ],
                          [
                            0.5555555555555556,
                            "#1f9e89"
                          ],
                          [
                            0.6666666666666666,
                            "#35b779"
                          ],
                          [
                            0.7777777777777778,
                            "#6ece58"
                          ],
                          [
                            0.8888888888888888,
                            "#b5de2b"
                          ],
                          [
                            1,
                            "#fde725"
                          ]
                        ],
                        "type": "heatmapgl"
                      }
                    ],
                    "histogram": [
                      {
                        "marker": {
                          "line": {
                            "color": "white",
                            "width": 0.6
                          }
                        },
                        "type": "histogram"
                      }
                    ],
                    "histogram2d": [
                      {
                        "colorbar": {
                          "outlinewidth": 1,
                          "tickcolor": "rgb(36,36,36)",
                          "ticks": "outside"
                        },
                        "colorscale": [
                          [
                            0,
                            "#440154"
                          ],
                          [
                            0.1111111111111111,
                            "#482878"
                          ],
                          [
                            0.2222222222222222,
                            "#3e4989"
                          ],
                          [
                            0.3333333333333333,
                            "#31688e"
                          ],
                          [
                            0.4444444444444444,
                            "#26828e"
                          ],
                          [
                            0.5555555555555556,
                            "#1f9e89"
                          ],
                          [
                            0.6666666666666666,
                            "#35b779"
                          ],
                          [
                            0.7777777777777778,
                            "#6ece58"
                          ],
                          [
                            0.8888888888888888,
                            "#b5de2b"
                          ],
                          [
                            1,
                            "#fde725"
                          ]
                        ],
                        "type": "histogram2d"
                      }
                    ],
                    "histogram2dcontour": [
                      {
                        "colorbar": {
                          "outlinewidth": 1,
                          "tickcolor": "rgb(36,36,36)",
                          "ticks": "outside"
                        },
                        "colorscale": [
                          [
                            0,
                            "#440154"
                          ],
                          [
                            0.1111111111111111,
                            "#482878"
                          ],
                          [
                            0.2222222222222222,
                            "#3e4989"
                          ],
                          [
                            0.3333333333333333,
                            "#31688e"
                          ],
                          [
                            0.4444444444444444,
                            "#26828e"
                          ],
                          [
                            0.5555555555555556,
                            "#1f9e89"
                          ],
                          [
                            0.6666666666666666,
                            "#35b779"
                          ],
                          [
                            0.7777777777777778,
                            "#6ece58"
                          ],
                          [
                            0.8888888888888888,
                            "#b5de2b"
                          ],
                          [
                            1,
                            "#fde725"
                          ]
                        ],
                        "type": "histogram2dcontour"
                      }
                    ],
                    "mesh3d": [
                      {
                        "colorbar": {
                          "outlinewidth": 1,
                          "tickcolor": "rgb(36,36,36)",
                          "ticks": "outside"
                        },
                        "type": "mesh3d"
                      }
                    ],
                    "parcoords": [
                      {
                        "line": {
                          "colorbar": {
                            "outlinewidth": 1,
                            "tickcolor": "rgb(36,36,36)",
                            "ticks": "outside"
                          }
                        },
                        "type": "parcoords"
                      }
                    ],
                    "pie": [
                      {
                        "automargin": true,
                        "type": "pie"
                      }
                    ],
                    "scatter": [
                      {
                        "fillpattern": {
                          "fillmode": "overlay",
                          "size": 10,
                          "solidity": 0.2
                        },
                        "type": "scatter"
                      }
                    ],
                    "scatter3d": [
                      {
                        "line": {
                          "colorbar": {
                            "outlinewidth": 1,
                            "tickcolor": "rgb(36,36,36)",
                            "ticks": "outside"
                          }
                        },
                        "marker": {
                          "colorbar": {
                            "outlinewidth": 1,
                            "tickcolor": "rgb(36,36,36)",
                            "ticks": "outside"
                          }
                        },
                        "type": "scatter3d"
                      }
                    ],
                    "scattercarpet": [
                      {
                        "marker": {
                          "colorbar": {
                            "outlinewidth": 1,
                            "tickcolor": "rgb(36,36,36)",
                            "ticks": "outside"
                          }
                        },
                        "type": "scattercarpet"
                      }
                    ],
                    "scattergeo": [
                      {
                        "marker": {
                          "colorbar": {
                            "outlinewidth": 1,
                            "tickcolor": "rgb(36,36,36)",
                            "ticks": "outside"
                          }
                        },
                        "type": "scattergeo"
                      }
                    ],
                    "scattergl": [
                      {
                        "marker": {
                          "colorbar": {
                            "outlinewidth": 1,
                            "tickcolor": "rgb(36,36,36)",
                            "ticks": "outside"
                          }
                        },
                        "type": "scattergl"
                      }
                    ],
                    "scattermapbox": [
                      {
                        "marker": {
                          "colorbar": {
                            "outlinewidth": 1,
                            "tickcolor": "rgb(36,36,36)",
                            "ticks": "outside"
                          }
                        },
                        "type": "scattermapbox"
                      }
                    ],
                    "scatterpolar": [
                      {
                        "marker": {
                          "colorbar": {
                            "outlinewidth": 1,
                            "tickcolor": "rgb(36,36,36)",
                            "ticks": "outside"
                          }
                        },
                        "type": "scatterpolar"
                      }
                    ],
                    "scatterpolargl": [
                      {
                        "marker": {
                          "colorbar": {
                            "outlinewidth": 1,
                            "tickcolor": "rgb(36,36,36)",
                            "ticks": "outside"
                          }
                        },
                        "type": "scatterpolargl"
                      }
                    ],
                    "scatterternary": [
                      {
                        "marker": {
                          "colorbar": {
                            "outlinewidth": 1,
                            "tickcolor": "rgb(36,36,36)",
                            "ticks": "outside"
                          }
                        },
                        "type": "scatterternary"
                      }
                    ],
                    "surface": [
                      {
                        "colorbar": {
                          "outlinewidth": 1,
                          "tickcolor": "rgb(36,36,36)",
                          "ticks": "outside"
                        },
                        "colorscale": [
                          [
                            0,
                            "#440154"
                          ],
                          [
                            0.1111111111111111,
                            "#482878"
                          ],
                          [
                            0.2222222222222222,
                            "#3e4989"
                          ],
                          [
                            0.3333333333333333,
                            "#31688e"
                          ],
                          [
                            0.4444444444444444,
                            "#26828e"
                          ],
                          [
                            0.5555555555555556,
                            "#1f9e89"
                          ],
                          [
                            0.6666666666666666,
                            "#35b779"
                          ],
                          [
                            0.7777777777777778,
                            "#6ece58"
                          ],
                          [
                            0.8888888888888888,
                            "#b5de2b"
                          ],
                          [
                            1,
                            "#fde725"
                          ]
                        ],
                        "type": "surface"
                      }
                    ],
                    "table": [
                      {
                        "cells": {
                          "fill": {
                            "color": "rgb(237,237,237)"
                          },
                          "line": {
                            "color": "white"
                          }
                        },
                        "header": {
                          "fill": {
                            "color": "rgb(217,217,217)"
                          },
                          "line": {
                            "color": "white"
                          }
                        },
                        "type": "table"
                      }
                    ]
                  },
                  "layout": {
                    "annotationdefaults": {
                      "arrowhead": 0,
                      "arrowwidth": 1
                    },
                    "autotypenumbers": "strict",
                    "coloraxis": {
                      "colorbar": {
                        "outlinewidth": 1,
                        "tickcolor": "rgb(36,36,36)",
                        "ticks": "outside"
                      }
                    },
                    "colorscale": {
                      "diverging": [
                        [
                          0,
                          "rgb(103,0,31)"
                        ],
                        [
                          0.1,
                          "rgb(178,24,43)"
                        ],
                        [
                          0.2,
                          "rgb(214,96,77)"
                        ],
                        [
                          0.3,
                          "rgb(244,165,130)"
                        ],
                        [
                          0.4,
                          "rgb(253,219,199)"
                        ],
                        [
                          0.5,
                          "rgb(247,247,247)"
                        ],
                        [
                          0.6,
                          "rgb(209,229,240)"
                        ],
                        [
                          0.7,
                          "rgb(146,197,222)"
                        ],
                        [
                          0.8,
                          "rgb(67,147,195)"
                        ],
                        [
                          0.9,
                          "rgb(33,102,172)"
                        ],
                        [
                          1,
                          "rgb(5,48,97)"
                        ]
                      ],
                      "sequential": [
                        [
                          0,
                          "#440154"
                        ],
                        [
                          0.1111111111111111,
                          "#482878"
                        ],
                        [
                          0.2222222222222222,
                          "#3e4989"
                        ],
                        [
                          0.3333333333333333,
                          "#31688e"
                        ],
                        [
                          0.4444444444444444,
                          "#26828e"
                        ],
                        [
                          0.5555555555555556,
                          "#1f9e89"
                        ],
                        [
                          0.6666666666666666,
                          "#35b779"
                        ],
                        [
                          0.7777777777777778,
                          "#6ece58"
                        ],
                        [
                          0.8888888888888888,
                          "#b5de2b"
                        ],
                        [
                          1,
                          "#fde725"
                        ]
                      ],
                      "sequentialminus": [
                        [
                          0,
                          "#440154"
                        ],
                        [
                          0.1111111111111111,
                          "#482878"
                        ],
                        [
                          0.2222222222222222,
                          "#3e4989"
                        ],
                        [
                          0.3333333333333333,
                          "#31688e"
                        ],
                        [
                          0.4444444444444444,
                          "#26828e"
                        ],
                        [
                          0.5555555555555556,
                          "#1f9e89"
                        ],
                        [
                          0.6666666666666666,
                          "#35b779"
                        ],
                        [
                          0.7777777777777778,
                          "#6ece58"
                        ],
                        [
                          0.8888888888888888,
                          "#b5de2b"
                        ],
                        [
                          1,
                          "#fde725"
                        ]
                      ]
                    },
                    "colorway": [
                      "#1F77B4",
                      "#FF7F0E",
                      "#2CA02C",
                      "#D62728",
                      "#9467BD",
                      "#8C564B",
                      "#E377C2",
                      "#7F7F7F",
                      "#BCBD22",
                      "#17BECF"
                    ],
                    "font": {
                      "color": "rgb(36,36,36)"
                    },
                    "geo": {
                      "bgcolor": "white",
                      "lakecolor": "white",
                      "landcolor": "white",
                      "showlakes": true,
                      "showland": true,
                      "subunitcolor": "white"
                    },
                    "hoverlabel": {
                      "align": "left"
                    },
                    "hovermode": "closest",
                    "mapbox": {
                      "style": "light"
                    },
                    "paper_bgcolor": "white",
                    "plot_bgcolor": "white",
                    "polar": {
                      "angularaxis": {
                        "gridcolor": "rgb(232,232,232)",
                        "linecolor": "rgb(36,36,36)",
                        "showgrid": false,
                        "showline": true,
                        "ticks": "outside"
                      },
                      "bgcolor": "white",
                      "radialaxis": {
                        "gridcolor": "rgb(232,232,232)",
                        "linecolor": "rgb(36,36,36)",
                        "showgrid": false,
                        "showline": true,
                        "ticks": "outside"
                      }
                    },
                    "scene": {
                      "xaxis": {
                        "backgroundcolor": "white",
                        "gridcolor": "rgb(232,232,232)",
                        "gridwidth": 2,
                        "linecolor": "rgb(36,36,36)",
                        "showbackground": true,
                        "showgrid": false,
                        "showline": true,
                        "ticks": "outside",
                        "zeroline": false,
                        "zerolinecolor": "rgb(36,36,36)"
                      },
                      "yaxis": {
                        "backgroundcolor": "white",
                        "gridcolor": "rgb(232,232,232)",
                        "gridwidth": 2,
                        "linecolor": "rgb(36,36,36)",
                        "showbackground": true,
                        "showgrid": false,
                        "showline": true,
                        "ticks": "outside",
                        "zeroline": false,
                        "zerolinecolor": "rgb(36,36,36)"
                      },
                      "zaxis": {
                        "backgroundcolor": "white",
                        "gridcolor": "rgb(232,232,232)",
                        "gridwidth": 2,
                        "linecolor": "rgb(36,36,36)",
                        "showbackground": true,
                        "showgrid": false,
                        "showline": true,
                        "ticks": "outside",
                        "zeroline": false,
                        "zerolinecolor": "rgb(36,36,36)"
                      }
                    },
                    "shapedefaults": {
                      "fillcolor": "black",
                      "line": {
                        "width": 0
                      },
                      "opacity": 0.3
                    },
                    "ternary": {
                      "aaxis": {
                        "gridcolor": "rgb(232,232,232)",
                        "linecolor": "rgb(36,36,36)",
                        "showgrid": false,
                        "showline": true,
                        "ticks": "outside"
                      },
                      "baxis": {
                        "gridcolor": "rgb(232,232,232)",
                        "linecolor": "rgb(36,36,36)",
                        "showgrid": false,
                        "showline": true,
                        "ticks": "outside"
                      },
                      "bgcolor": "white",
                      "caxis": {
                        "gridcolor": "rgb(232,232,232)",
                        "linecolor": "rgb(36,36,36)",
                        "showgrid": false,
                        "showline": true,
                        "ticks": "outside"
                      }
                    },
                    "title": {
                      "x": 0.05
                    },
                    "xaxis": {
                      "automargin": true,
                      "gridcolor": "rgb(232,232,232)",
                      "linecolor": "rgb(36,36,36)",
                      "showgrid": false,
                      "showline": true,
                      "ticks": "outside",
                      "title": {
                        "standoff": 15
                      },
                      "zeroline": false,
                      "zerolinecolor": "rgb(36,36,36)"
                    },
                    "yaxis": {
                      "automargin": true,
                      "gridcolor": "rgb(232,232,232)",
                      "linecolor": "rgb(36,36,36)",
                      "showgrid": false,
                      "showline": true,
                      "ticks": "outside",
                      "title": {
                        "standoff": 15
                      },
                      "zeroline": false,
                      "zerolinecolor": "rgb(36,36,36)"
                    }
                  }
                },
                "title": {
                  "font": {
                    "color": "Black",
                    "size": 22
                  },
                  "text": "<b>Documents and Topics</b>",
                  "x": 0.5,
                  "xanchor": "center",
                  "yanchor": "top"
                },
                "width": 1200,
                "xaxis": {
                  "visible": false
                },
                "yaxis": {
                  "visible": false
                }
              }
            }
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "# ⭐ UMAP可视化\n",
        "reduced_embeddings = UMAP(n_neighbors=5, n_components=2, min_dist=0.0, metric='cosine',).fit_transform(embeddings)\n",
        "topic_model.visualize_documents(docs, reduced_embeddings=reduced_embeddings, hide_annotations=True)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.6"
    },
    "orig_nbformat": 4
  },
  "nbformat": 4,
  "nbformat_minor": 2
}
